<?php
class Cms_Model_DAO_Banner
{    
	private $_db;
	private static $_instance = null;
 
	/**
	 * @return Cms_Model_DAO_Banner
	 * */
	public static function factory() {
		if(self::$_instance == null) {
			self::$_instance = new self();
		}
		
		return self::$_instance;
	}
	
    private function __construct()
    {
    	$module = Utils_Global::get('module');
		$this->_db = Utils_Global::getDbInstance($module);
    }
	
    /**
     * Get list of articles by given options
     * @param array $options
     * @return array
     * */
	public function getBanner($options=array()) {
		$sql = "SELECT `image`, `link`, `name`, `note`,
						`width`, `height`, `hash_folder`,`video`,`flash` ";
		$from = " FROM `banner` INNER JOIN `position_banner` ON `banner`.`id` = `position_banner`.`banner_id` ";
		$where = " WHERE active = 1 ";
		if($options['position']) {
			$where .= " AND `position_banner`.`pos_id` = {$this->_db->quote($options['position'], 'INTEGER')} ";
		}
		
		$limitQuery = "";
		if(isset($options['offset']) && $options['offset'] >= 0 && $options['limit'] > 0) {
		    $limitQuery = " LIMIT {$this->_db->quote($options['offset'], 'INTEGER')}, {$this->_db->quote($options['limit'], 'INTEGER')} ";
		}
		$order = " ORDER BY `order` ASC ";
		$result = $this->_db->fetchAll( $sql . $from . $where . $order . $limitQuery);
		return $result;
	}
	
	/**
	 * Get banners count by given options
	 * @param array $options
	 * @return integer
	 * */
	public function getBannerCount($options = array()) {
	    $sql = "SELECT count(1) as `count` ";
	    $from = " FROM `banner` INNER JOIN `position_banner` ON `banner`.`id` = `position_banner`.`banner_id` ";
	    $where = " WHERE active = 1 ";
	    if($options['position']) {
	    	$where .= " AND `position_banner`.`pos_id` = {$this->_db->quote($options['position'], 'INTEGER')} ";
	    }
	    
	    $result = $this->_db->fetchOne( $sql . $from . $where);
	    return $result;
	}
	
	public function getAlbums($options = array()) {
	    $sql = "SELECT `p`.`id`, `p`.`position_name` `album_name`, `p`.`image` as `album_image`, `banner`.`image`, `link`, `name`, `note`,
						`width`, `height`, `hash_folder`,`video`,`flash` ";
		$from = " FROM `position` p INNER JOIN `position_banner` ON `p`.`id` = `position_banner`.`pos_id`
		                            INNER JOIN `banner` ON `banner`.`id` = `position_banner`.`banner_id` ";
		$where = " WHERE active = 1 AND `p`.`collection` = 2 ";
		
		$limitQuery = "";
		if(isset($options['offset']) && $options['offset'] >= 0 && $options['limit'] > 0) {
		    $limitQuery = " LIMIT {$this->_db->quote($options['offset'], 'INTEGER')}, {$this->_db->quote($options['limit'], 'INTEGER')} ";
		}
		$order = " ORDER BY `order` ASC ";
		$result = $this->_db->fetchAll( $sql . $from . $where . $order . $limitQuery);
		return $result;
	}
	
	public function getAlbumCount() {
	    $sql = "SELECT count(1) as `count` ";
	    $from = " FROM `position` ";
	    $where = " WHERE `position`.`collection` = 2 ";
	     
	    $result = $this->_db->fetchOne( $sql . $from . $where);
	    return $result;
	}
}